package com.itcast.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itcast.health.pojo.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.itcast.health.pojo.Menu;
import com.itcast.service.MenuService;

import java.util.List;
import java.util.Map;

/**
 * @author Gao   Fu
 * @version v1.0
 * @date 2019/12/3 11:06
 * @description
 **/
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/getUsername")
    public Result getUsername(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (user!=null){
            return new Result(true,"",user.getUsername());
        }else{
            return new Result(false,"登录失败");
        }

    }






    @RequestMapping("/getactivemenu")

    //动态获得菜单
    public Result getactivemenu(){
        //根据上下文获取登录用户名
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = user.getUsername();
        try {
            //调用菜单服务
            List<Menu> menuList=menuService.getMenuByusername(username);
            return new Result(true,"",menuList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"");
        }
    }


    /**
     * 获取所有菜单并返回前端页面
     * @return
     */
    @RequestMapping("getMenu")
    public Result getMenu(){
        try {
            List<Menu> menus=menuService.getMenu();
            return new Result(true,"",menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"");
        }

    }


    /**
     * 删除功能
     * @param id 菜单menuId
     * @return
     */
   @RequestMapping("delete")
    public Result delete(Integer id){

       try {
         menuService.delete(id);
           return new Result(true,"删除成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(true,"删除失败");
       }
   }


   @RequestMapping("getMenuById")
    public Result getMenuById(Integer id){
       Map map= null;
       try {
           map = menuService.getMenuById(id);
           return new Result(true,"",map);
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(true,"",map);
       }
   }

    /**
     * 编辑菜单所有数据
     * @param childIds
     * @param roleIds
     * @param menu
     * @return
     */
   @RequestMapping("edit")
    public Result edit(Integer[]childIds, Integer[]roleIds, @RequestBody Menu menu){
       try {
           menuService.edit(childIds,roleIds,menu);
           return new Result(true,"编辑菜单成功");
       } catch (Exception e) {
           e.printStackTrace();
           return new Result(false,"编辑菜单失败");
       }
   }
}






